package ru.urfc.portal.rest;

import java.util.List;

import javax.ejb.EJB;
import javax.ejb.Stateless;
import javax.servlet.http.HttpServletResponse;
import javax.ws.rs.Consumes;
import javax.ws.rs.DELETE;
import javax.ws.rs.GET;
import javax.ws.rs.POST;
import javax.ws.rs.PUT;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.core.Context;
import javax.ws.rs.core.MediaType;

import ru.urfc.portal.dao.BasePersistence;
import ru.urfc.portal.managers.ClientManager;
import ru.urfc.portal.model.admins.User;
import ru.urfc.portal.model.admins.UserCreate;
import ru.urfc.portal.model.admins.UserUpdate;
import ru.urfc.portal.model.audit.AuditLogEntry;
import ru.urfc.portal.ui.client.Constants;

@Stateless
@Produces({MediaType.APPLICATION_XML, MediaType.TEXT_XML, MediaType.APPLICATION_JSON})
@Consumes({MediaType.APPLICATION_XML, MediaType.TEXT_XML, MediaType.APPLICATION_JSON})
@Path(Constants.CLIENTS + "/{clientId}/" + Constants.CLIENT_ASSOCIATED_USERS)
public class AssociatedUsersService extends BaseService {
	
	@EJB BasePersistence basePersistence;
	
	@EJB ClientManager clientManager;
	
	@GET
	public List<User> listUsers(@PathParam("clientId") Long clientId) {
		return clientManager.getUsers(clientId);
	}
	
	@POST
	public void createUser(@PathParam("clientId") Long clientId, UserCreate user, @Context HttpServletResponse response){
		clientManager.createUser(clientId, user);
	}
	
	@GET
	@Path("{userId}")
	public User getUser(@PathParam("clientId") Long clientId, @PathParam("userId") Long userId) {
		return clientManager.getUser(clientId, userId);
	}

	@PUT
	@Path("{userId}")
	public User updateUser(@PathParam("clientId") Long clientId, @PathParam("userId") Long userId, UserUpdate userUpdate) {
		return clientManager.updateUser(clientId, userId, userUpdate);
	}

	@DELETE
	@Path("{userId}")
	public void deleteUser(@PathParam("clientId") Long clientId, @PathParam("userId") Long userId) {
		clientManager.deleteUser(clientId, userId);
	}

	@GET
	@Path("{userId}/" + AUDIT_LOG)
	public List<AuditLogEntry> listAuditLogByUser(@PathParam("clientId") Long clientId,
			@PathParam("userId") Long userId) {
		return new ClientService().getSubService().listAuditLog(clientId);
	}

}
